﻿using Advance.Repositories.EntityFrameworkCore.Models;
using Advanced.Domain.Core;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Advance.Repositories.EntityFrameworkCore
{
    public class ApplicationDbContextSeed
    {
        private UserManager<ApplicationUser> _userManager;
        private RoleManager<ApplicationUserRole> _userRoleManager;
        public async System.Threading.Tasks.Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider services)
        {
            _userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
            _userRoleManager = services.GetRequiredService<RoleManager<ApplicationUserRole>>();

            if (!_userRoleManager.Roles.Any())
            {
                var role = new ApplicationUserRole()
                {
                    Name = "administrators",
                    NormalizedName = "administrators"
                };
                await _userRoleManager.CreateAsync(role);
            }
            if (!_userManager.Users.Any())
            {
                var user = new ApplicationUser()
                {
                    Password = "123456",
                    UserName = "Administrator",
                    Email = "519564415@qq.com",
                    SecurityStamp = "p@ssw0rd",
                    NormalizedUserName = "admin",
                    Avatar = "https://gitee.com/uploads/18/1183118_gainorloss_259.png?1483611120",
                };
                await _userManager.CreateAsync(user);
                await _userManager.AddToRoleAsync(user, "administrators");
            }
        }
    }
}
